package com.bw.zg3park2212.park

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.bw.base.entity.ParkAddParam
import com.bw.base.entity.ParkTypeEntity
import com.bw.base.entity.ParkTypeEntityItem
import com.bw.base.mvvm.BaseViewModel
import com.bw.base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow

import javax.inject.Inject

@HiltViewModel
class ParkViewModel @Inject constructor(val  apiService: ApiService):BaseViewModel() {
    fun getParkType(para:String,type:String):Flow<PagingData<ParkTypeEntityItem>>{
        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
            ParkDataSource(apiService,para,type)
        }).flow.cachedIn(viewModelScope)
    }
    val parkdelSuccse=MutableLiveData<Boolean>()
    fun parkdel(id:Int){
        httpRequst(
            {apiService.delePark(id)},
            parkdelSuccse::postValue
        )
    }
    val parkupSuccse=MutableLiveData<ParkTypeEntityItem>()
    fun parkup(id:Int,body:ParkAddParam){
        httpRequst(
            {apiService.upPark(id,body) },
            parkupSuccse::postValue
        )
    }
}